// view/my/addzzhgl/addzzhgl.js
let app = getApp()
let yuming = app.globalData.yuming;
let mobile = app.globalData.mobile;
let account = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    buttonClicked: false,
    yuming: yuming,
    heights: '',
    title: "",
    list2: [],
    dianlist: [],
    diandex: 0,
    state: '', //0添加 1修改
    name: '', //姓名
    zhanghao: '', //账号
    password: '', //密码
    numId: '',
    dpid: '', //店铺id
    id: '',
    dianpguo: '请选择店铺',
    ischong: true,
  },
  // 验证
  yz() {
    if (this.data.dianpguo == '请选择店铺') {
      wx.showToast({
        title: '请选择店铺',
        icon: 'none'
      })
    } else if (this.data.name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
    } else if (this.data.zhanghao == '') {
      wx.showToast({
        title: '请输入账号',
        icon: 'none'
      })
    } else if (!account.test(this.data.zhanghao) && !mobile.test(this.data.zhanghao)) {
      wx.showToast({
        title: '请输入6-16位数字加英文的账号或手机号',
        icon: 'none'
      })
    } else if (this.data.state == 0 && this.data.password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
    } else if (this.data.password != '' && !account.test(this.data.password)) {
      wx.showToast({
        title: '请输入6-16位数字加英文的密码',
        icon: 'none'
      })
    } else {
      if (this.data.state == 0) {
        this.tijiao();
      } else {
        this.xiugai();
      }
    }
  },
  // 修改
  xiugai() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'shop/ShopSaveSubSon', {
      id: that.data.id,
      shopId: wx.getStorageSync('shopId'),
      userName: that.data.name,
      account: that.data.zhanghao,
      password: that.data.password,
      jurisdiction: that.data.numId,
      sId: that.data.dpid
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        console.log(res.data.data);
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 提交
  tijiao() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'shop/shopAddSubSon', {
      shopId: wx.getStorageSync('shopId'),
      userName: that.data.name,
      account: that.data.zhanghao,
      password: that.data.password,
      jurisdiction: that.data.numId,
      sId: that.data.dpid
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 姓名
  name(e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 账号
  zhanghao(e) {
    this.setData({
      zhanghao: e.detail.value
    })
  },
  // 密码
  password(e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 店铺
  bindian(e) {
    let index = e.detail.value
    this.setData({
      dianpguo: this.data.dianlist[index].shopName,
      dpid: this.data.dianlist[index].id
    });
  },
  // 选择栏目
  list_xiang(e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.list2;
    var numId = [];
    item[index].isChange = !item[index].isChange;
    for (var i = 0; i < item.length; i++) {
      if (item[i].isChange) {
        numId.push(item[i].id);
      }
    }
    numId = numId.join(',');
    numId = ',' + numId;
    this.setData({
      numId,
      list2: item
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 栏目列表
  lmlist() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/ShopSelectCartlist', {
      total: 1,
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      wx.hideLoading();
      var data = res.data.data;
      var numIds = [];
      for (var i = 0; i < data.length; i++) {
        numIds.push(data[i].id);
        data[i].isChange = true;
      }
      let numId = numIds.join(',');
      numId = ',' + numId;
      that.setData({
        list2: res.data.data,
        numId
      })
    })
  },
  // 店铺列表
  dplist() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/shopUsersList', {
      mobile: wx.getStorageSync('mobile')
    }, 'POST').then(res => {
      wx.hideLoading();
      that.setData({
        dianlist: res.data.data
      })
    })
  },
  // 重置密码
  chongzhi() {
    this.setData({
      ischong: false
    })
  },
  // 修改详情
  xq() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/ShopFindSubSon', {
      id: that.data.id
    }, 'POST').then(res => {
      wx.hideLoading();
      var data = res.data.data
      var jurisdiction = res.data.data.jurisdiction.split(',')
      var list2 = that.data.list2
      for (let i = 0; i < list2.length; i++) {
        list2[i].isChange = false
        for (let j = 0; j < jurisdiction.length; j++) {
          if (jurisdiction[j] == list2[i].id) {
            list2[i].isChange = true
          }
        }
      }
      that.setData({
        zhanghao: data.account,
        name: data.userName,
        dianpguo: data.shopName,
        dpid: data.sId,
        list2: list2,
        numId: data.jurisdiction
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      state: options.state,
      id: options.id
    })
    this.lmlist()
    this.dplist()
    if (this.data.state == 1) {
      this.xq();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})